var ibMarketShop = {
	init: function()
	{
		Debug.write("Initializing ibm.shop.js");
		
		document.observe("dom:loaded", function(){
			ibMarketShop.setUpCategoryList();
		});
	},
	
	setUpCategoryList: function()
	{
		if(!$('subCategoryBlock'))
		{
			return;
		}
		
		ibMarketShop.categoryTable = $$('.ipb_table')[0];
		ibMarketShop.categoryTableWrapper = $( ibMarketShop.categoryTable ).wrap( 'div' );
		$( ibMarketShop.categoryTableWrapper ).addClassName('table_wrap');
		
		$('subCategoryHeader').select('.toggle')[0].observe( 'click', ibMarketShop.toggleCat );			
		
		cookie = ipb.Cookie.get('marketCats');
		
		if( cookie )
		{
			ibMarketShop.categoryTableWrapper.hide();
			$('subCategoryHeader').addClassName('collapsed');
		}
	},
	
	/* ------------------------------ */
	/**
	 * Show/hide a category
	 * 
	 * @var		{event}		e	The event
	*/
	toggleCat: function(e)
	{
		if( ibMarketShop.animating ){ return; }
		
		var click = Event.element(e);
		var remove = $A();
		$( ibMarketShop.categoryTableWrapper ).identify(); // IE8 fix
		
		ibMarketShop.animating = true;
		
		Effect.toggle( ibMarketShop.categoryTableWrapper, 'blind', {duration: 0.4, afterFinish: function(){ ibMarketShop.animating = false; } } );
		
		if( $('subCategoryHeader').hasClassName('collapsed') )
		{
			$('subCategoryHeader').removeClassName('collapsed');
			ipb.Cookie.doDelete('marketCats');
		}
		else
		{
			new Effect.Morph( $('subCategoryHeader'), {style: 'collapsed', duration: 0.4, afterFinish: function(){
				$('subCategoryHeader').addClassName('collapsed');
				ibMarketShop.animating = false;
			} });
			ipb.Cookie.set('marketCats', 1, 1);
		}
		
		Event.stop( e );
	}
}

ibMarketShop.init();